package com.halloon.android.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.halloon.android.R;
import com.halloon.android.bean.ProfileBean;
import com.halloon.android.data.ContentManager;
import com.halloon.android.data.DBManager;
import com.halloon.android.data.SettingsManager;
import com.halloon.android.image.ImageLoader;
import com.halloon.android.util.NumberUtil;
import com.halloon.android.view.ScrollTextView;
import com.halloon.android.ui.fragment.TabMainPageFragment.MainPageFragmentCallback;

public class TabProfileFragment extends SherlockFragment implements OnClickListener{
	
    private ProfileFragmentCallback pCallback;
	private ImageView myHeadicon;
	private TextView myNick;
	private TextView myName;
	private ImageView mySex;
	private TextView myAddress;
	private TextView mySign;
	private TextView myInter;
	private TextView tweetNum;
	private TextView fansNum;
	private TextView idolNum;
	private TextView favNum;
	private Button sendButton;
	private Button editButton;
	private Button backButton;
	//private TextView last_tweet;
	private Context context;
	
	private ProfileBean profileBean;
	
	public static final int ME = 0;
	public static final int OTHER = 1;
	
	private int type = ME;
	
	private String name;
	private String nick;
	
	public interface ProfileFragmentCallback{
		public void destroyProfileFragment();
		public void setupTweetListFragment(Bundle bundle);
	}

	@Override
	public void onAttach(Activity activity){
		super.onAttach(activity);
		this.pCallback = (ProfileFragmentCallback) activity;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.tab_profile, null, false);
		
		myHeadicon = (ImageView) root.findViewById(R.id.my_headicon);
		myNick = (TextView) root.findViewById(R.id.my_nick);
		mySex = (ImageView) root.findViewById(R.id.my_sex);
		myName = (TextView) root.findViewById(R.id.my_name);
		myAddress = (TextView) root.findViewById(R.id.my_address);
		mySign = (ScrollTextView) root.findViewById(R.id.my_sign);
		myInter = (TextView) root.findViewById(R.id.my_inter);
		tweetNum = (TextView) root.findViewById(R.id.tweet_num);
		fansNum = (TextView) root.findViewById(R.id.fans_num);
		idolNum = (TextView) root.findViewById(R.id.idol_num);
		favNum = (TextView) root.findViewById(R.id.fav_num);
		sendButton = (Button) root.findViewById(R.id.send_message_button);
		editButton = (Button) root.findViewById(R.id.edit_button);
		backButton = (Button) root.findViewById(R.id.back_button);
		//last_tweet=(TextView) root.findViewById(R.id.last_tweet);
		
		if(getArguments().getString("name") != null) this.name = getArguments().getString("name");
		
		tweetNum.setClickable(true);
		tweetNum.setOnClickListener(this);
		if(type == OTHER){
			sendButton.setVisibility(View.VISIBLE);
			backButton.setVisibility(View.VISIBLE);
			editButton.setVisibility(View.GONE);
			sendButton.setOnClickListener(this);
			backButton.setOnClickListener(this);
			myHeadicon.setOnClickListener(this);
		}else{
			sendButton.setVisibility(View.GONE);
			backButton.setVisibility(View.GONE);
			editButton.setVisibility(View.VISIBLE);
			editButton.setOnClickListener(this);
		}
		
		updateProfile();

		root.setOnClickListener(this);
		
		return root;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		context = getSherlockActivity();

	}
	
	public void updateProfile(){
		AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>(){
			
			@Override
			protected String doInBackground(Void... arg0){
				
				if(type == ME){
					if(SettingsManager.getInstance(context).getProfileStatus() == DBManager.PROFILE_STATUS_INIT){
				        profileBean = ContentManager.getInstance(context).getMyProfile();
				        DBManager.getInstance(context).upgradeProfile(profileBean);
					}else{
						profileBean = DBManager.getInstance(context).getProfile();
						if(profileBean == null || profileBean.getNick() == null){
							profileBean = ContentManager.getInstance(context).getMyProfile();
							DBManager.getInstance(context).upgradeProfile(profileBean);
						}
					}
				}else{
					profileBean = ContentManager.getInstance(context).getOtherProfile(name, null);
					nick = profileBean.getNick();
				}
					
				return null;
			}
			
			@Override
			protected void onPostExecute(String result){
					
				ImageLoader.getInstance(context).displayImage(profileBean.getHead()+"/100", myHeadicon, 12);
				myNick.setText(profileBean.getNick());
				if(profileBean.getSex().equals("男")){
					mySex.setVisibility(View.VISIBLE);
					mySex.setImageResource(R.drawable.wb_icon_male);
				}else if(profileBean.getSex().equals("女")){
					mySex.setVisibility(View.VISIBLE);
					mySex.setImageResource(R.drawable.wb_icon_female);
				}else{
					mySex.setVisibility(View.GONE);
				}
				myName.setText(context.getString(R.string.account) + ":"+profileBean.getName());
				myAddress.setText(profileBean.getLocation()); 
				mySign.setText(profileBean.getIntroduction());
				myInter.setText(profileBean.getTag());
				
				tweetNum.setText(Html.fromHtml("<font color='0x0e396e'>" + context.getString(R.string.wblog) + "</font><br/>" + NumberUtil.shortenNumericString(profileBean.getTweetNum())));
				fansNum.setText(Html.fromHtml("<font color='0x0e396e'>" + context.getString(R.string.fan)+ "</font><br/>" + NumberUtil.shortenNumericString(profileBean.getFansNum())));
				idolNum.setText(Html.fromHtml("<font color='0x0e396e'>" + context.getString(R.string.idol) + "</font><br/>" + NumberUtil.shortenNumericString(profileBean.getIdolNum())));
				favNum.setText(Html.fromHtml("<font color='0x0e396e'>" + context.getString(R.string.fav) + "</font><br/>" + NumberUtil.shortenNumericString(profileBean.getFavNum())));

				//last_tweet.append(profileBean.getTweetBean().getText());
				
				super.onPostExecute(result);
			}
		};
		
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
  			task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
  		} else {
  			task.execute();
  		}
		
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.tweet_num:
			Bundle bundle = new Bundle();
			bundle.putString("name", name);
			bundle.putString("nick", nick);
			pCallback.setupTweetListFragment(bundle);
			break;
		case R.id.send_message_button:
			break;
		case R.id.back_button:
			pCallback.destroyProfileFragment();
			break;
		case R.id.edit_button:
			break;
		case R.id.my_headicon:
			((MainPageFragmentCallback) context).setupPictureDialog(profileBean.getHead(), "/100");
			break;
		}
	}
	
	public void setType(int type){
		this.type = type;
	}
	
}
